﻿using Acme.SmartWMS.Common;
using Acme.SmartWMS.Inbound;
using Acme.SmartWMS.Outbound.CustomerShipmentService.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.Outbound.CustomerShipmentService
{
    public class CustomerShipmentAppService:ApplicationService,ICustomerShipmentAppService
    {
        /// <summary>
        /// 客户出货仓储
        /// </summary>
        private readonly IRepository<CustomerShipment,int> _customerShipmentRepository;
        public CustomerShipmentAppService(IRepository<CustomerShipment, int> customerShipmentRepository)
        {
            _customerShipmentRepository = customerShipmentRepository;
        }
        /// <summary>
        /// 获取客户出货显示 查询 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="shipmentNumber"></param>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetCustomerShipmentList(int pageIndex, int pageSize, string? shipmentNumber, string? productCode)
        {
            var predicate = PredicateBuilder.New<CustomerShipment>(true);
            if (!string.IsNullOrEmpty(shipmentNumber))
            {
                predicate = predicate.And(x => x.ShipmentNumber.Contains(shipmentNumber));
            }
            if (!string.IsNullOrEmpty(productCode))
            {
                predicate = predicate.And(x => x.ProductCode.Contains(productCode));
            }
            var dto = await _customerShipmentRepository.GetListAsync(predicate);
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / pageSize);
            result.CurrentPage = pageIndex;
            result.PageSize = pageSize;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            return result;
        }
        /// <summary>
        /// 添加客户出货
        /// </summary>
        /// <param name="customerShipmentDTO"></param>
        /// <returns></returns>
        public async Task<int> AddCustomerShipment(CustomerShipmentDTO customerShipmentDTO)
        {
            var shipmentNumber=_customerShipmentRepository.CountAsync(x=>x.ShipmentNumber==customerShipmentDTO.ShipmentNumber).Result;
            if (shipmentNumber > 0)
            {
                return -1;
            }
            var productCode = _customerShipmentRepository.CountAsync(x => x.ProductCode==customerShipmentDTO.ProductCode).Result;
            if (productCode > 0)
            {
                return -2;
            }
            var customerShipment = customerShipmentDTO.MapTo<CustomerShipment>();
            await _customerShipmentRepository.InsertAsync(customerShipment);
            return 1;
        }
        /// <summary>
        /// 删除客户出货
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteCustomerShipment(string ids)
        {
            string[]  array= ids.Split(',');
            await _customerShipmentRepository.DeleteAsync(x => ids.Contains(x.Id.ToString()));
            return 1;
        }
        /// <summary>
        /// 获取客户出货主键Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CustomerShipment> GetCustomerShipmentById(int id)
        {
            var customerShipment = await _customerShipmentRepository.GetAsync(x => x.Id == id);
            var customerShipments=customerShipment.MapTo<CustomerShipment>();
            return customerShipments;
        }
        /// <summary>
        /// 更新客户出货
        /// </summary>
        /// <param name="customerShipmentDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateCustomerShipment(CustomerShipmentDTO customerShipmentDTO)
        {
            var shipmentNumber = _customerShipmentRepository.CountAsync(x => x.ShipmentNumber == customerShipmentDTO.ShipmentNumber).Result;
            if (shipmentNumber > 0)
            {
                return -1;
            }
            var productCode = _customerShipmentRepository.CountAsync(x => x.ProductCode == customerShipmentDTO.ProductCode).Result;
            if (productCode > 0)
            {
                return -2;
            }
            var customerShipment=customerShipmentDTO.MapTo<CustomerShipment>();
            await _customerShipmentRepository.UpdateAsync(customerShipment);
            return 1;
        }
    }
}
